home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / Goodies / CPreferences.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-04  |  3.8 KB  |  134 lines  |  [TEXT/KAHL]

  1. #include <TCL>
  2. #include "GLOBAL.h"
  3. #include "CPreferences.h"
  4. #include "CDiagnostic.h"
  5. #include "CUtility.h"
  6. #include "STR.h"
  7. #include "chars.h"            /*    Needed for SIGNATURE. */
  8.  
  9. NEW void CPreferences::IPreferences()
  10. {
  11.     SysEnvRec theWorld;
  12.     HParamBlockRec params;
  13.     StringPtr folderName, fileName;
  14.     int sysVolNum, dirID, folderVRefNum;
  15.     CInfoPBRec cInfo;
  16.     FInfo fInfo;
  17.     Boolean created;
  18.     
  19.   /*This code mostly lifted from CInstance by Sven Axelsson, which means to
  20.     say that I don't understand much of it... (and hence didn't spot the
  21.     bugs - thanks to Martin Minow for setting me right). */
  22.  
  23.   /*Get system volume information (I guess...). */
  24.     SysEnvirons(1, &theWorld);
  25.     g_Diagnostic("theWorld.sysVRefNum: %d", theWorld.sysVRefNum);
  26.  
  27.     params.volumeParam.ioCompletion = NULL;
  28.     params.volumeParam.ioNamePtr = NULL /*sysVolName*/;
  29.     params.volumeParam.ioVRefNum = theWorld.sysVRefNum;
  30.     params.volumeParam.ioVolIndex = 0;
  31.     gUtility->HardCheckOSError(PBHGetVInfo(¶ms, FALSE));
  32.     sysVolNum = params.volumeParam.ioVRefNum;
  33.     g_Diagnostic("sysVolNum: %d", sysVolNum);
  34.  
  35.   /*Find the preferences folder. */
  36.     folderName = gUtility->BriefGetString(STRcommon, PREFERENCES_index);
  37.         /*    The folder's called "Preferences" or whatever's in the STR#...
  38.             N.B. We can't call BriefGetString again while we still need
  39.             folderName. */
  40.  
  41.     cInfo.dirInfo.ioCompletion = NULL;
  42.     cInfo.dirInfo.ioNamePtr = folderName;
  43.     cInfo.dirInfo.ioVRefNum = theWorld.sysVRefNum;
  44.     cInfo.dirInfo.ioFDirIndex = 0;
  45.     cInfo.dirInfo.ioDrDirID = 0;
  46.  
  47.     if (PBGetCatInfo(&cInfo, FALSE) == fnfErr) {
  48.         g_Diagnostic("Prefs folder does not exist");
  49.       /*Folder does not exist; create it. */
  50.         params.fileParam.ioCompletion = NULL;
  51.         params.fileParam.ioNamePtr = folderName;
  52.         params.fileParam.ioVRefNum = theWorld.sysVRefNum;
  53.         params.fileParam.ioFVersNum = 0;
  54.         params.fileParam.ioFDirIndex = 0;
  55.         params.fileParam.ioDirID = 0;
  56.         gUtility->HardCheckOSError(PBDirCreate(¶ms, FALSE));
  57.         dirID = params.fileParam.ioDirID;
  58.     } else {
  59.         g_Diagnostic("Prefs folder exists");
  60.         dirID = cInfo.dirInfo.ioDrDirID;
  61.     }
  62.     
  63.   /*Find the preference file in preference folder, or create it. */
  64.     fileName = gUtility->ApplicationName();
  65.             /*    We give the file the same name as the application. */
  66.     if (HGetFInfo(sysVolNum, dirID, fileName, &fInfo) == fnfErr) {
  67.         g_Diagnostic("Prefs file does not exist");
  68.         created = TRUE;
  69.         gUtility->HardCheckOSError(
  70.             HCreate(sysVolNum, dirID, fileName, SIGNATURE, PREFS_TYPE)
  71.         );
  72.     } else {
  73.         g_Diagnostic("Prefs file exists");
  74.         created = FALSE;
  75.     }
  76.  
  77.     gUtility->HardCheckOSError(OpenWD(sysVolNum, dirID, 0, &folderVRefNum));
  78.  
  79.     if (created) {
  80.         HCreateResFile(sysVolNum, dirID, fileName);
  81.         gUtility->HardCheckResError();
  82.     }
  83.  
  84.     itsFileRefNum = HOpenResFile(sysVolNum, dirID, fileName, fsRdWrPerm);
  85.     if (itsFileRefNum == -1)  gUtility->HardCheckResError();
  86. }
  87.  
  88. PRIVATE Handle CPreferences::Find(ResType type, int ID)
  89. {
  90.     Handle han = Get1Resource(type, ID);
  91.     OSErr err;
  92.  
  93.     if (han == NULL) {        /*    Either we can't find it, or we had an error. */
  94.         err = ResError();
  95.         if (err == noErr || err == resNotFound)
  96.             return NULL;    /*    Couldn't find it (not a real problem). */
  97.         else
  98.             gUtility->HardCheckOSError(err);
  99.                             /*    A real error of some kind... */
  100.     } else                    /*    Success. */
  101.         return han;
  102. }
  103.  
  104. NEW Handle CPreferences::Get(ResType type, int ID)
  105. {
  106.     Handle han = Find(type, ID);
  107.  
  108.     if (han != NULL)  DetachResource(han);
  109.     return han;
  110. }
  111.  
  112. NEW void CPreferences::Put(Handle han, ResType type, int ID, StringPtr name)
  113. {
  114.     Handle old = Find(type, ID);
  115.  
  116.     if (old != NULL) {        /*    Delete old one. Can this cause fragmentation? */
  117.         RmveResource(old);
  118.         gUtility->HardCheckResError();
  119.         DisposHandle(old);
  120.     }
  121.  
  122.     HandToHand(&han);        /*    Take a copy, so we don't foul up the handle. */
  123.     AddResource(han, type, ID, name);
  124.     gUtility->HardCheckResError();
  125. }
  126.  
  127. OVERRIDE void CPreferences::Dispose()
  128. {
  129.     CloseResFile(itsFileRefNum);
  130.     gUtility->HardCheckResError();
  131.  
  132.     inherited::Dispose();
  133. }
  134.